import LDataGoldManager from "./datas/LDataGoldManager";
import Common from "./common/Common";
import GameManager from "./manager/GameManager";
import SoundManager from "./manager/SoundManager";
import Define from "./common/Define";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass} = cc._decorator;

@ccclass
export default class GoldCtr extends cc.Component {

    private rigidBody:cc.RigidBody;
    //奖励金币的数量
    goldCount:number = 0;
    //是否可以收集
    isCanCollect:boolean = true;

    isCanFly:boolean = true;

    isCanUseTag:boolean = false;
    goldId:number = 0;

    static goldTextArr:cc.Label[] = [];

    onLoad () {
        this.rigidBody = this.node.getComponent<cc.RigidBody>(cc.RigidBody);
        this.rigidBody.active = false;
    }

    setGoldId(id:number){
        this.node.setScale(1,1)
        this.goldCount = LDataGoldManager.GetDataById(id).goldnumber;
        this.isCanUseTag = false;
        this.isCanCollect = true;
        this.isCanFly = true;
        this.node.active = true;
        this.rigidBody.gravityScale = 1;
        this.goldId = id

        if(Common.getRandom(0,2) == 0){
            this.rigidBody.linearVelocity = cc.v2(Common.getRandom(100,350)*-1,Common.getRandom(100,350));
        }else{
            this.rigidBody.linearVelocity = cc.v2(Common.getRandom(100,350),Common.getRandom(100,350));
        }
    }
    
    onBeginContact(contact: cc.PhysicsContact, selfCollider:  cc.PhysicsCollider, otherCollider:  cc.PhysicsCollider): void{
        let other:string = otherCollider.node.name;
        if(other.indexOf("cannon") == 0 && this.isCanCollect){
            //SoundManager.palySoundById(Define.soundGetcoin)
            this.isCanCollect = false
            this.showTextEffect()
        }
    }

    showTextEffect(){
        let nodeText = cc.instantiate(Common.nodeAddGold);
        this.node.getParent().addChild(nodeText);
        nodeText.position = this.node.position;
        nodeText.active = true;
        nodeText.getComponent<cc.Label>(cc.Label).string = "+"+this.goldCount.toString();
        
        let x = nodeText.position.x
        let y = nodeText.position.y
        let action = cc.sequence(cc.moveTo(0.8,new cc.Vec2(x,y + Common.getRandom(200,250))),cc.callFunc(function(){
            nodeText.destroy();
        }))
        nodeText.runAction(action)
        nodeText.color = Define.goldTextRGBArr[this.goldId]
        Common.actionLeftRightRotate(nodeText)
    }
    
    update(dt){
       if(!this.isCanCollect){
            this.rigidBody.active = false;
            if(this.isCanFly){
                this.fly();
            }  
       }else{
            this.rigidBody.active = true;
       }
    }
    //金币飞行
    fly(){
        GameManager.instance.handlerCollectGold(this.goldCount);
        this.node.setScale(0.5,0.5);
        this.node.runAction(
            cc.sequence(cc.moveTo(0.5,new cc.Vec2(50,1085)).easing(cc.easeIn(1)),
            cc.callFunc(function(){
                this.isCanUseTag = true;
                this.node.active = false
            }.bind(this))
        ));
        this.isCanFly = false;
    }

    setIcon(icon:cc.SpriteFrame){
        this.node.getComponent<cc.Sprite>(cc.Sprite).spriteFrame = icon;
    }

    autoCollect(){
        if(!this.isCanUseTag){
            this.isCanCollect = false
        }
    }

    clear(){
        this.isCanCollect = false
        this.isCanUseTag = true;
        this.node.active = false
    }
}
